package com.asia5b.a5mall.shop.activity

import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.asia5b.a5bmall.resource.ActCons
import com.asia5b.a5mall.middleware.base.SimpleActivity
import com.asia5b.a5mall.middleware.logic.viewmodel.shop.SearchShopActivityVM
import com.asia5b.a5mall.middleware.model.base.ResultModel
import com.asia5b.a5mall.middleware.model.shop.AbsShopModel
import com.asia5b.a5mall.middleware.utils.push
import com.asia5b.a5mall.shop.adapter.ShopListAdapter
import com.asia5b.a5mall.shop.databinding.ActivitySearchshopBinding
import com.asia5b.a5mall.middleware.widget.refresh.AutoRefreshLayout
import com.scwang.smartrefresh.layout.api.RefreshLayout
import kotlinx.android.synthetic.main.activity_searchshop.*

/**
 * Created by zhangyuncai on 2020/1/11.
 * 搜索商家展示列表
 */
@Route(path = ActCons.SearchShopActivity)
class SearchShopActivity : SimpleActivity<SearchShopActivityVM, ActivitySearchshopBinding>(){
    override val viewModelClazz: Class<SearchShopActivityVM>
        get() = SearchShopActivityVM::class.java

    override fun getDataBinding(): ActivitySearchshopBinding {
        return ActivitySearchshopBinding.inflate(layoutInflater)
    }
    override var sIsDark = true

    val mRecyclerView by lazy { rv_shoplist }
    val mDatas = arrayListOf<AbsShopModel>()
    val mAdapter by lazy { ShopListAdapter(mActivity, mDatas) }

    var keyWord: String? = null

    val etSearch by lazy { et_shoplist_key }


    override fun initView() {
        mSmartRefreshLayout=mDataBinding.autoShoplist
        mDataBinding.autoShoplist.setEnableRefresh(false)
        keyWord = intent.getStringExtra("keyword")
        etSearch.setText(keyWord)

        mRecyclerView.run {
            layoutManager = LinearLayoutManager(mActivity)
            adapter = mAdapter
        }

        getDatas(0)
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        getDatas(2)
    }
    override fun initEvent() {

//        etSearch.setOnEditorActionListener { v, actionId, event ->
//            if (etSearch.text.isNotEmpty()) {
//                keyWord = etSearch.text.toString()
//                getDatas(0)
//                return@setOnEditorActionListener true
//            }
//            return@setOnEditorActionListener false
//        }
        ll_shoplist_vessel.setOnClickListener {
            push(ActCons.SearchKeyActivity)
        }
    }

    override fun startObserver() {
        mViewModel.getDatasResultLiveData.observe(this, Observer {
            getDatasResult(it)
        })
    }
    override fun initData() {

    }
    fun getDatas(refresh: Int) {
        mViewModel.getDatas(refresh, "${keyWord}")
    }

     fun getDatasResult(resultModel: ResultModel<ArrayList<AbsShopModel>>) {
        if (resultModel.page <= 1) {
            mDatas.clear()
        }
        if (!resultModel.data.isNullOrEmpty()) {
            mAdapter.addData(resultModel.data!!)
        }
        mDataBinding.autoShoplist.setEnableLoadMore(resultModel.page < resultModel.sumPage)
    }
}